//
//  MPReplyToolBarView.m
//  Picture
//
//  Created by Killer on 2016/11/29.
//  Copyright © 2016年 孙玉琢. All rights reserved.
//

#import "MPReplyToolBarView.h"

#import "CONST.h"


@interface MPReplyToolBarView ()


@end
@implementation MPReplyToolBarView
#pragma mark -
- (void)initNotionCenter
{
    [MPNotificationCenter addObserver:self selector:@selector(textViewEditChanged:) name:UITextViewTextDidChangeNotification object:_textView];
}
- (void)dealloc
{
    [MPNotificationCenter removeObserver:self];
}
#pragma mark -
- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        [self initView];
        [self initNotionCenter];
    }
    return self;
}
#pragma mark -
- (void)initView
{
    
    self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    CGFloat left = 15;
    CGFloat width = ScreenWidth - left*2;
    CGFloat top = 20;
    CGFloat bgH = 75;
    
    UIView *bgVi = [[UIView alloc]initWithFrame:CGRectMake(left, top, width, bgH)];
    bgVi.backgroundColor = [UIColor whiteColor];
    bgVi.layer.borderColor = Line_COLOR.CGColor;
    bgVi.layer.borderWidth = 1;
    [self addSubview:bgVi];
    self.bgView = bgVi;
    
    [self initBgView];
    CGFloat bottomT = _bgView.bottom;
    self.height = bottomT+15;
}
- (void)initBgView
{
    CGFloat bgH = _bgView.height;
    CGFloat bgW = _bgView.width;
    PlaceholderTextView *contentView = [[PlaceholderTextView alloc]initWithFrame:CGRectMake(0, 0, bgW,bgH)];
    contentView.font = [UIFont  systemFontOfSize:15];
    contentView.placeholder = @"字数限制为15个字符";
    contentView.placeholderColor = [UIColor lightGrayColor];
    contentView.placeholderFont = [UIFont  systemFontOfSize:15];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.delegate = self;
    contentView.returnKeyType = UIReturnKeyDone;
    [_bgView addSubview:contentView];
    self.textView = contentView;
}
#pragma mark- 监听文本改变
//限制输入文本长度===常常由于联想输入的缘故，会有很多字符一起输入
-(void)textViewEditChanged:(NSNotification *)obj{
    
    UITextView *textView = (UITextView *)obj.object;
    if (textView.text.length >15 && textView.markedTextRange == nil) {
        textView.text = [textView.text substringToIndex:15];
    }
}
//判断用户点击Done
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
   replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        self.textViewText(textView.text);
        return NO;
    }
    return YES;
}
@end
